layui.use(['form', 'jquery'], function () {

    var form = layui.form,
        $ = layui.jquery;

    let id = sessionStorage.getItem("userRoleId");
    $("#id").val(id);
    findAllRole();

    /**
     * 查询单个
     * @param id
     */
    function findById(id) {
        let result = commonAjax('/user/findById', {id: id}, 'get');
        $('#userRole').val(result.data.userRole);
        $('#team').val(result.data.team);
        //刷新选中框样式需要添加下面这段话
        form.render();
    }

    /**
     * 保存
     */
    form.on('submit(commit)', function (data) {
        let formData = JSON.stringify(data.field);

        let result = commonAjax('/user/update', formData, 'post');
        if (result.count === 1) {
            layerMsgSaveSuccess();
        } else {
            layerMsgSaveFail();
        }

        return false;
    });

    /**
     * 查询所有角色
     */
    function findAllRole() {
        let data = {
            pageNum: 1,
            pageSize: 100
        };
        let result = commonAjax('/role/findAll', data, 'get');
        let html = '';
        for (let i = 0; i < result.data.length; i++) {
            html += '<option value="' + result.data[i].id + '">' + result.data[i].roleName + '</option>';
        }
        $("#userRole").html(html);

        findById(id);
    }

});